﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using Northwind.NET.Model;
using System.Data.Common;

namespace Northwind.NET.EDM.FE.WinForms.Controls
{
    public partial class CustomersUserControl : BaseUserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadData();
        }

        public CustomersUserControl()
        {
            InitializeComponent();

            
        }

        public override string FormCaption { get { return Settings.Resources.CustomersFormCaption; } }
        public override dynamic GetEntitySet(ObjectContext context)
        {
            return new Model.Customer();
        }

        public override BindingSource UserControlBindingSource { get { return customerBindingSource; } }

        private void LoadData()
        {
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnectionString;
                DbDataAdapter adapter = Factory.CreateDataAdapter();
                DbCommand command = Factory.CreateCommand();
                command.Connection = connection;
                command.CommandText = "SELECT ID, Name, ContactName, ContactTitle, Address, City, " +
                            "Region, PostalCode, Country, Phone, Fax FROM customers";

                adapter.SelectCommand = command;

                DataSet dataset = new DataSet();
                dataset.Tables.Add("Customers");
                adapter.Fill(dataset.Tables["Customers"]);

                this.customerBindingSource.DataSource = dataset.Tables["Customers"];


            }
        }

    }
}
